﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Reflection;
using System.Collections.Specialized;
using System.Collections.Generic;

namespace TripLOA.GUI.service.rest.tester
{
    public partial class RestTester : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            classNameDiv.Visible = false;
            methodNameDiv.Visible = false;
            
            classSelectDiv.Visible = true;
            methodSelectDiv.Visible = false;
            testDiv.Visible = false;
        }

        protected void classSubmit_Click(object sender, EventArgs e)
        {
            //Select class
            Type type = Type.GetType(classSelect.Text);

            classNameDiv.Visible = true;
            classNameLabel.Text = classSelect.Text;

            //Show select method form

            MethodInfo[] methods = type.GetMethods();

            foreach (var m in methods)
            {
                methodSelect.Items.Add(m.Name);
            }


            classSelectDiv.Visible = false;
            methodSelectDiv.Visible = true;
            testDiv.Visible = false;
        }

        protected void methodSubmit_Click(object sender, EventArgs e)
        {
            //Test values intert
            Type type = Type.GetType(classSelect.Text);
            MethodInfo method = type.GetMethod(methodSelect.Text);

            ParameterInfo[] pars = method.GetParameters();

            var controls = parametersListDiv.Controls;

            HiddenField redirect = new HiddenField();
            redirect.ID = "redirect";
            switch (classSelect.Text)
            {
                case "TripLOA.WebService.TripCore":
                    redirect.Value = "/service/rest/RestTrip.ashx";
                    break;

                case "TripLOA.WebService.UserCore":
                    redirect.Value = "/service/rest/RestUser.ashx";
                    break;

                case "TripLOA.WebService.GeoRefCore":
                    redirect.Value = "/service/rest/RestGeoRef.ashx";
                    break;
            }

            HiddenField queryMethod = new HiddenField();
            queryMethod.ID = "method";
            queryMethod.Value = method.Name;

            parametersListDiv.Controls.Add(redirect);
            parametersListDiv.Controls.Add(queryMethod);

            for (int i = 0; i < pars.Count(); i++)
            {
                string parName = "";
                char[] parArray = pars[i].Name.ToCharArray();
                for (int j = 0; j < parArray.Length; j++)
                {
                    if (parArray[j] != char.ToLower(parArray[j]))
                    {
                        parName += "_" + char.ToLower(parArray[j]);
                    }
                    else
                    {
                        parName += parArray[j];
                    }
                }

                HtmlGenericControl div = new HtmlGenericControl("div");
                HtmlGenericControl txt = new HtmlGenericControl("div");
                txt.Style["float"] = "left";
                txt.Style["width"] = "200px";
                txt.InnerText = parName+": ";
                TextBox box = new TextBox();
                box.ID = parName;

                div.Controls.Add(txt);
                div.Controls.Add(box);

                controls.Add(div);

                classNameDiv.Visible = true;
                methodNameDiv.Visible = true;
                methodNameLabel.Text = method.Name;
            }

            classSelectDiv.Visible = false;
            methodSelectDiv.Visible = false;
            testDiv.Visible = true;
        }

        protected void testSubmit_Click(object sender, EventArgs e) 
        {
            //Get Params
            NameValueCollection _REQUEST = this.Context.Request.Params;

            //debugLabel.Text = _REQUEST.ToString();

            classNameDiv.Visible = true;
            methodNameDiv.Visible = true;

            classSelectDiv.Visible = false;
            methodSelectDiv.Visible = false;
            testDiv.Visible = false;

            this.Context.Response.Redirect(_REQUEST["redirect"]+"?"+_REQUEST.ToString());
        }
    }
}
